<!DOCTYPE HTML>
<html lang="zh-CN">


<head><meta name="generator" content="Hexo 3.8.0">
    <meta charset="utf-8">
    <meta name="keywords" content="【Linux】设备树实例分析, ZhengN_Blog">
    <meta name="description" content="前言我们可以从LED程序中榨取很多知识：基本的驱动框架、驱动的简单分层、驱动的分层+分离思想、总线设备驱动模型、设备树等。这大多都是结合韦老师的教程学的，这篇笔记结合第6个demo（基于设备树）来学习、分析：

框图下面是LED程序的几个层">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>【Linux】设备树实例分析 | ZhengN_Blog</title>
    <link rel="icon" type="image/jpeg" href="/favicon.jpg">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">
    <style type="text/css">
        
    </style>

    <script src="/libs/jquery/jquery-2.2.0.min.js"></script>
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head>


<body>

<header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">ZhengN_Blog</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fa fa-navicon"></i></a>
<ul class="right">
    
    <li class="hide-on-med-and-down">
        <a href="/" class="waves-effect waves-light">
            
            <i class="fa fa-home"></i>
            
            <span>首页</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/tags" class="waves-effect waves-light">
            
            <i class="fa fa-tags"></i>
            
            <span>标签</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/categories" class="waves-effect waves-light">
            
            <i class="fa fa-bookmark"></i>
            
            <span>分类</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/archives" class="waves-effect waves-light">
            
            <i class="fa fa-archive"></i>
            
            <span>归档</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/about" class="waves-effect waves-light">
            
            <i class="fa fa-user-circle-o"></i>
            
            <span>关于</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/friends" class="waves-effect waves-light">
            
            <i class="fa fa-address-book"></i>
            
            <span>友情链接</span>
        </a>
    </li>
    
    <li>
        <a href="#searchModal" class="modal-trigger waves-effect waves-light">
            <i id="searchIcon" class="fa fa-search" title="搜索"></i>
        </a>
    </li>
</ul>

<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">ZhengN_Blog</div>
        <div class="logo-desc">
            
            printf(&#34;每天进步一点点~&#34;);
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li>
            <a href="/" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-home"></i>
                
                首页
            </a>
        </li>
        
        <li>
            <a href="/tags" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-tags"></i>
                
                标签
            </a>
        </li>
        
        <li>
            <a href="/categories" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-bookmark"></i>
                
                分类
            </a>
        </li>
        
        <li>
            <a href="/archives" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-archive"></i>
                
                归档
            </a>
        </li>
        
        <li>
            <a href="/about" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-user-circle-o"></i>
                
                关于
            </a>
        </li>
        
        <li>
            <a href="/friends" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-address-book"></i>
                
                友情链接
            </a>
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://github.com/zhengnianli/stm32_weather" class="waves-effect waves-light" target="_blank">
                <i class="fa fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>

        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://github.com/zhengnianli/stm32_weather" class="github-corner tooltipped hide-on-med-and-down" target="_blank" data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewbox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"/>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"/>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"/>
    </svg>
</a>
        
    </nav>

</header>





<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/22.jpg')">
    <div class="container">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <div class="description center-align post-title">
                        【Linux】设备树实例分析
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>



<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #toc-content .is-active-link::before {
        background-color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 20px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/Linux/" target="_blank">
                                <span class="chip bg-color">Linux</span>
                            </a>
                        
                            <a href="/tags/设备树/" target="_blank">
                                <span class="chip bg-color">设备树</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fa fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/Linux/" class="post-category" target="_blank">
                                Linux
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                <div class="post-date info-break-policy">
                    <i class="fa fa-calendar-minus-o fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2020-04-23
                </div>

                
                    
                    <div class="info-break-policy">
                        <i class="fa fa-file-word-o fa-fw"></i>文章字数:&nbsp;&nbsp;
                        1.9k
                    </div>
                    

                    
                    <div class="info-break-policy">
                        <i class="fa fa-clock-o fa-fw"></i>阅读时长:&nbsp;&nbsp;
                        7 分
                    </div>
                    
                
				
				
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="fa fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">
        <div class="card-content article-card-content">
            <div id="articleContent">
                <h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>我们可以从LED程序中榨取很多知识：基本的驱动框架、驱动的简单分层、驱动的分层+分离思想、总线设备驱动模型、设备树等。这大多都是结合韦老师的教程学的，这篇笔记结合第6个demo（基于设备树）来学习、分析：</p>
<p><img src="https://s1.ax1x.com/2020/04/22/JUrO4U.png" alt></p>
<h2 id="框图"><a href="#框图" class="headerlink" title="框图"></a>框图</h2><p>下面是LED程序的几个层次结构图：</p>
<p><img src="https://s1.ax1x.com/2020/04/22/JUrqEV.png" alt><br><img src="https://s1.ax1x.com/2020/04/22/JUrxgJ.png" alt><br><img src="https://s1.ax1x.com/2020/04/22/JUrLNT.png" alt></p>
<p><img src="https://s1.ax1x.com/2020/04/22/JUrjCF.png" alt><br><img src="https://s1.ax1x.com/2020/04/22/JUrzv9.png" alt></p>
<blockquote>
<p>注意：层与层之间的箭头指向是相对的，从哪指向哪看你怎么理解。比如有两个函数：函数A和函数B，我们可以说函数A调用函数B，也可以说函数B被函数A调用。</p>
</blockquote>
<p>本篇笔记基于第⑤个图来分析。</p>
<h2 id="体验设备树"><a href="#体验设备树" class="headerlink" title="体验设备树"></a>体验设备树</h2><p>我们先来体验一下使用设备树描述引脚信息的方式来点灯。以百问网开发板为例，修改内核目录Linux-4.9.88/arch/arm/boot/dts下的100ask_imx6ull-14x14.dts（百问科技开发板出厂带的设备树文件）。</p>
<p>把出厂带的设备树文件的led相关节点给屏蔽掉，然后添加如下节点信息至根节点：</p>
<pre class=" language-c"><code class="language-c"><span class="token macro property">#<span class="token directive keyword">define</span> GROUP_PIN(g,p) ((g&lt;&lt;16) | (p))</span>
100ask_led@<span class="token number">0</span> <span class="token punctuation">{</span>
    compatible <span class="token operator">=</span> <span class="token string">"100as,leddrv"</span><span class="token punctuation">;</span>
    pin <span class="token operator">=</span> <span class="token operator">&lt;</span><span class="token function">GROUP_PIN</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token operator">></span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span></code></pre>
<p>修改后的设备树文件内容如：</p>
<p><img src="https://s1.ax1x.com/2020/04/22/JUspuR.png" alt></p>
<p>在内核根目录下使用如下命令编译设备树源文件：</p>
<blockquote>
<p>make dtbs V=1 </p>
</blockquote>
<p><img src="https://s1.ax1x.com/2020/04/22/JUrv34.png" alt></p>
<p>然后把设备树文件与可加载的led驱动模块、led应用程序上传到板子里：</p>
<p><img src="https://s1.ax1x.com/2020/04/22/JUs9D1.png" alt></p>
<p>上传成功的文件如下：</p>
<p><img src="https://s1.ax1x.com/2020/04/22/JUsFUK.png" alt></p>
<p>运行测试：</p>
<p><img src="https://s1.ax1x.com/2020/04/22/JUsCHx.png" alt></p>
<h2 id="实验过程分析"><a href="#实验过程分析" class="headerlink" title="实验过程分析"></a>实验过程分析</h2><p>这个实验的led驱动同样依赖的是<code>总线设备驱动模型</code>，我们在<a href="http://mp.weixin.qq.com/s?__biz=MzU5MzcyMjI4MA==&mid=100003346&idx=1&sn=20ca3d01d296ec99def8910291bc0754&chksm=7e0d60d5497ae9c3adb7b1a5f492cd5c1965121ac85ed22c1a58636318542ce69bd191535fa3#rd" target="_blank" rel="noopener">【Linux笔记】总线设备驱动模型</a>中也有提到描述设备有两种方法：一种是直接用platform_device结构体来指定，另一种是用设备树来指定。在本次实验中我们就是用设备树来描述设备。</p>
<p>之前我们用platform_device结构体来指定设备信息时，platform_driver是直接从platform_device结构体里拿资源的，如：</p>
<p><img src="https://s1.ax1x.com/2020/04/22/JUsiE6.png" alt><br><img src="https://s1.ax1x.com/2020/04/22/JUsV8e.png" alt></p>
<p>现在我们用设备树来指定设备信息时，platform_driver是如何获取相关资源的呢？大致过程如下：</p>
<p><img src="https://s1.ax1x.com/2020/04/22/JUsk4O.png" alt><br><img src="https://s1.ax1x.com/2020/04/22/JUsECD.png" alt><br><img src="https://s1.ax1x.com/2020/04/22/JUsuDI.png" alt></p>
<p>这里我们还需要注意的一点是：并不是所有的设备树节点都可以转换为platform_device。下面看看几条规则：</p>
<ul>
<li><p>根节点下含有 compatile 属性的子节点<code>能转换为platform_device</code>；</p>
</li>
<li><p>含有特定 compatile 属性（它的值是 “simple-bus”,”simplemfd”,”isa”,”arm,amba-bus” 四者之一）的节点的子节点<code>能转换为platform_device</code>；</p>
</li>
<li><p>I2C、 SPI 总线节点下的子节点 <code>不不不能转换为platform_device</code>，这些总线下的子节点， 应该交给对应的总线驱动程序来处理。</p>
</li>
</ul>
<p>下面看一个例子：</p>
<p><img src="https://s1.ax1x.com/2020/04/22/JUsnKA.png" alt></p>
<p>接下来，我们简单来看一下platform_device与platform_driver匹配的函数：</p>
<p><img src="https://s1.ax1x.com/2020/04/22/JUsevd.png" alt></p>
<p>这里有几种匹配方式，其它几种匹配方式在之前的笔记<a href="http://mp.weixin.qq.com/s?__biz=MzU5MzcyMjI4MA==&mid=100003346&idx=1&sn=20ca3d01d296ec99def8910291bc0754&chksm=7e0d60d5497ae9c3adb7b1a5f492cd5c1965121ac85ed22c1a58636318542ce69bd191535fa3#rd" target="_blank" rel="noopener">【Linux笔记】总线设备驱动模型</a>中也有简单地分析过。这里，我们来看第二种匹配方式（使用设备树时的匹配方式）。下面看看具体如何匹配：</p>
<p><img src="https://s1.ax1x.com/2020/04/22/JUsZgH.png" alt></p>
<p>其中过程①优先匹配，其次是过程②，最后是过程③。但是，实际上现在主要使用的是过程①的匹配，即匹配compatible属性。过程②与过程③已经过时了，Linux内核不推荐使用这两种匹配方法。这一点我们在上一篇的笔记<a href="http://mp.weixin.qq.com/s?__biz=MzU5MzcyMjI4MA==&mid=100003404&idx=1&sn=a45f3cd291dad44408af3eec99b5534e&chksm=7e0d608b497ae99dad1f20b639a3fa0d587d7ca87f28a439411caf78f2ade6ba2d40cfd93e22#rd" target="_blank" rel="noopener">【Linux笔记】设备树基础知识</a>中也有简单提到。</p>
<p>在本次实验中，我们的匹配示意图如下：</p>
<p><img src="https://s1.ax1x.com/2020/04/22/JUsKbt.png" alt></p>
<h2 id="实验代码"><a href="#实验代码" class="headerlink" title="实验代码"></a>实验代码</h2><p>代码大多与之前的<a href="http://mp.weixin.qq.com/s?__biz=MzU5MzcyMjI4MA==&mid=100003379&idx=1&sn=cc8ef6eeb4fa3d4ff87e950786821e98&chksm=7e0d60f4497ae9e24006b796c8a16610f41b324dbd907a3dc3d7a5c48d19b0c2eaa7ae75c3fc#rd" target="_blank" rel="noopener">【Linux笔记】LED驱动（总线设备驱动模型）</a>中的代码一样，这里也来简单看一下。</p>
<h3 id="1、应用程序ledtest-c："><a href="#1、应用程序ledtest-c：" class="headerlink" title="1、应用程序ledtest.c："></a>1、应用程序ledtest.c：</h3><pre class=" language-c"><code class="language-c"><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword">int</span> argc<span class="token punctuation">,</span> <span class="token keyword">char</span> <span class="token operator">*</span><span class="token operator">*</span>argv<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">int</span> fd<span class="token punctuation">;</span>
    <span class="token keyword">char</span> status<span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/* 1. 判断参数 */</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>argc <span class="token operator">!=</span> <span class="token number">3</span><span class="token punctuation">)</span> 
    <span class="token punctuation">{</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"Usage: %s &lt;dev> &lt;on | off>\n"</span><span class="token punctuation">,</span> argv<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/* 2. 打开文件 */</span>
    fd <span class="token operator">=</span> <span class="token function">open</span><span class="token punctuation">(</span>argv<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> O_RDWR<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>fd <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"can not open file %s\n"</span><span class="token punctuation">,</span> argv<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/* 3. 写文件 */</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token number">0</span> <span class="token operator">==</span> <span class="token function">strcmp</span><span class="token punctuation">(</span>argv<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">"on"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        status <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token function">write</span><span class="token punctuation">(</span>fd<span class="token punctuation">,</span> <span class="token operator">&amp;</span>status<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">else</span>
    <span class="token punctuation">{</span>
        status <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token function">write</span><span class="token punctuation">(</span>fd<span class="token punctuation">,</span> <span class="token operator">&amp;</span>status<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token function">close</span><span class="token punctuation">(</span>fd<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>运行测试命令：</p>
<pre class=" language-c"><code class="language-c"><span class="token punctuation">.</span><span class="token operator">/</span>ledtest <span class="token operator">/</span>dev<span class="token operator">/</span>100ask_led0 on
<span class="token punctuation">.</span><span class="token operator">/</span>ledtest <span class="token operator">/</span>dev<span class="token operator">/</span>100ask_led0 off</code></pre>
<p><code>int main(int argc, char **argv)</code>形式的main函数相关笔记：<a href="http://mp.weixin.qq.com/s?__biz=MzU5MzcyMjI4MA==&mid=100001834&idx=2&sn=a9649172617e52d126f327fd43bb831b&chksm=7e0d66ed497aeffb1b56cf8d337de66cf954593a8929897364bf8dff6441daf6de1810b76e4c#rd" target="_blank" rel="noopener">main函数的几种形式</a>。</p>
<h3 id="2、驱动层leddrv-c"><a href="#2、驱动层leddrv-c" class="headerlink" title="2、驱动层leddrv.c"></a>2、驱动层leddrv.c</h3><p>这一层主要是放一些通用的驱动操作函数，核心代码如：</p>
<p><strong>驱动程序入口函数：</strong></p>
<p><img src="https://s1.ax1x.com/2020/04/22/JUsQVP.png" alt></p>
<p><strong>open、write函数：</strong></p>
<p><img src="https://s1.ax1x.com/2020/04/22/JUs8PS.png" alt></p>
<p><strong>其它代码：</strong></p>
<p><img src="https://s1.ax1x.com/2020/04/22/JUs158.png" alt></p>
<p>其中led的操作结构体如下：</p>
<p><img src="https://s1.ax1x.com/2020/04/22/JUslUf.png" alt></p>
<h3 id="3、硬件层：chip-demo-gpio-c"><a href="#3、硬件层：chip-demo-gpio-c" class="headerlink" title="3、硬件层：chip_demo_gpio.c"></a>3、硬件层：chip_demo_gpio.c</h3><p>这一层主要是一些寄存器相关的操作，及platform_driver相关。与上一个实验代码不同的部分就是这个文件。</p>
<p><strong>（1）驱动初始化函数：</strong></p>
<p><img src="https://s1.ax1x.com/2020/04/22/JUsYvj.png" alt></p>
<p><strong>（2）probe函数：</strong></p>
<p>当设备树的compatible属性与platform_driver中的设备匹配表中的compatible成员互相匹配时会执行此函数获取设备信息。</p>
<p><img src="https://s1.ax1x.com/2020/04/22/JUsG8g.png" alt></p>
<p>这里的pin属性与compatible属性（标准属性）类别不同，pin属性是个自定义属性。我们可以使用<code>of_property_read_u32</code>函数来获取这些自定义属性的内容。</p>
<p>与设备树相关的读取函数我们在上一篇笔记<a href="http://mp.weixin.qq.com/s?__biz=MzU5MzcyMjI4MA==&mid=100003404&idx=1&sn=a45f3cd291dad44408af3eec99b5534e&chksm=7e0d608b497ae99dad1f20b639a3fa0d587d7ca87f28a439411caf78f2ade6ba2d40cfd93e22#rd" target="_blank" rel="noopener">【Linux笔记】设备树基础知识</a>中也有详细介绍。这些函数大多在文件 <code>include/linux/of.h</code> 中可以找到：</p>
<p><img src="https://s1.ax1x.com/2020/04/22/JUsJ2Q.png" alt></p>
<p><strong>（3）led寄存器操作相关的代码：</strong></p>
<pre class=" language-c"><code class="language-c"><span class="token comment" spellcheck="true">/* 寄存器物理地址 */</span>
<span class="token macro property">#<span class="token directive keyword">define</span> CCM_CCGR1_BASE                (0X020C406C)    </span>
<span class="token macro property">#<span class="token directive keyword">define</span> SW_MUX_GPIO5_IO03_BASE        (0X02290014)</span>
<span class="token macro property">#<span class="token directive keyword">define</span> GPIO5_DR_BASE                (0X020AC000)</span>
<span class="token macro property">#<span class="token directive keyword">define</span> GPIO5_GDIR_BASE                (0X020AC004)</span>

<span class="token comment" spellcheck="true">/* 映射后的寄存器虚拟地址指针 */</span>
<span class="token keyword">static</span> <span class="token keyword">void</span> __iomem <span class="token operator">*</span>CCM_CCGR1<span class="token punctuation">;</span>
<span class="token keyword">static</span> <span class="token keyword">void</span> __iomem <span class="token operator">*</span>SW_MUX_GPIO5_IO03<span class="token punctuation">;</span>
<span class="token keyword">static</span> <span class="token keyword">void</span> __iomem <span class="token operator">*</span>GPIO5_DR<span class="token punctuation">;</span>
<span class="token keyword">static</span> <span class="token keyword">void</span> __iomem <span class="token operator">*</span>GPIO5_GDIR<span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">/* 初始化LED, which-哪个LED */</span>    
<span class="token keyword">static</span> <span class="token keyword">int</span> <span class="token function">board_demo_led_init</span> <span class="token punctuation">(</span><span class="token keyword">int</span> which<span class="token punctuation">)</span>    
<span class="token punctuation">{</span>   
    <span class="token keyword">int</span> group<span class="token punctuation">,</span> pin<span class="token punctuation">;</span>
    <span class="token keyword">unsigned</span> <span class="token keyword">int</span> val<span class="token punctuation">;</span>

    group <span class="token operator">=</span> <span class="token function">GROUP</span><span class="token punctuation">(</span>g_ledpins<span class="token punctuation">[</span>which<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    pin <span class="token operator">=</span> <span class="token function">PIN</span><span class="token punctuation">(</span>g_ledpins<span class="token punctuation">[</span>which<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">printk</span><span class="token punctuation">(</span><span class="token string">"init gpio: group %d, pin %d\n"</span><span class="token punctuation">,</span> group<span class="token punctuation">,</span> pin<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/* 100ask_IMX6uLL_Board LED：GPIO5_3 */</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">5</span> <span class="token operator">==</span> group<span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token punctuation">(</span><span class="token number">3</span> <span class="token operator">==</span> pin<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token comment" spellcheck="true">/* 相关寄存器物理地址与虚拟地址之间的映射 */</span>
        <span class="token comment" spellcheck="true">/* 1、地址映射：时钟寄存器 */</span>
        CCM_CCGR1 <span class="token operator">=</span> <span class="token function">ioremap</span><span class="token punctuation">(</span>CCM_CCGR1_BASE<span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>        
        <span class="token comment" spellcheck="true">/* 2、地址映射：模式寄存器 */</span>    
        SW_MUX_GPIO5_IO03 <span class="token operator">=</span> <span class="token function">ioremap</span><span class="token punctuation">(</span>SW_MUX_GPIO5_IO03_BASE<span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
        <span class="token comment" spellcheck="true">/* 3、地址映射：数据寄存器 */</span>
        GPIO5_DR <span class="token operator">=</span> <span class="token function">ioremap</span><span class="token punctuation">(</span>GPIO5_DR_BASE<span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>    
        <span class="token comment" spellcheck="true">/* 地址映射：方向寄存器 */</span>
        GPIO5_GDIR <span class="token operator">=</span> <span class="token function">ioremap</span><span class="token punctuation">(</span>GPIO5_GDIR_BASE<span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment" spellcheck="true">/* 使能GPIO5时钟 */</span>
        val <span class="token operator">=</span> <span class="token function">readl</span><span class="token punctuation">(</span>CCM_CCGR1<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">/* 读出当前CCM_CCGR1配置值 */</span>
        val <span class="token operator">&amp;</span><span class="token operator">=</span> <span class="token operator">~</span><span class="token punctuation">(</span><span class="token number">3</span> <span class="token operator">&lt;&lt;</span> <span class="token number">30</span><span class="token punctuation">)</span><span class="token punctuation">;</span>        <span class="token comment" spellcheck="true">/* 清除以前的设置 */</span>
        val <span class="token operator">|</span><span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">3</span> <span class="token operator">&lt;&lt;</span> <span class="token number">30</span><span class="token punctuation">)</span><span class="token punctuation">;</span>        <span class="token comment" spellcheck="true">/* 设置新值 */</span>
        <span class="token function">writel</span><span class="token punctuation">(</span>val<span class="token punctuation">,</span> CCM_CCGR1<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment" spellcheck="true">/* 设置GPIO5_IO03的为IO模式 */</span>
        <span class="token function">writel</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> SW_MUX_GPIO5_IO03<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment" spellcheck="true">/* 设置GPIO5_IO03方向为输出 */</span>
        val <span class="token operator">=</span> <span class="token function">readl</span><span class="token punctuation">(</span>GPIO5_GDIR<span class="token punctuation">)</span><span class="token punctuation">;</span> 
        val <span class="token operator">&amp;</span><span class="token operator">=</span> <span class="token operator">~</span><span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">&lt;&lt;</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>         
        val <span class="token operator">|</span><span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">&lt;&lt;</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>         
        <span class="token function">writel</span><span class="token punctuation">(</span>val<span class="token punctuation">,</span> GPIO5_GDIR<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">else</span>
    <span class="token punctuation">{</span>
        <span class="token function">printk</span><span class="token punctuation">(</span><span class="token string">"This is not 100ask_IMX6ULL_Board!\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">/* 控制LED, which-哪个LED, status:1-亮,0-灭 */</span>
<span class="token keyword">static</span> <span class="token keyword">int</span> <span class="token function">board_demo_led_ctl</span> <span class="token punctuation">(</span><span class="token keyword">int</span> which<span class="token punctuation">,</span> <span class="token keyword">char</span> status<span class="token punctuation">)</span> 
<span class="token punctuation">{</span>
    <span class="token keyword">int</span> group<span class="token punctuation">,</span> pin<span class="token punctuation">;</span>
    <span class="token keyword">unsigned</span> <span class="token keyword">int</span> val<span class="token punctuation">;</span>

    group <span class="token operator">=</span> <span class="token function">GROUP</span><span class="token punctuation">(</span>g_ledpins<span class="token punctuation">[</span>which<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    pin <span class="token operator">=</span> <span class="token function">PIN</span><span class="token punctuation">(</span>g_ledpins<span class="token punctuation">[</span>which<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">printk</span><span class="token punctuation">(</span><span class="token string">"init gpio: group %d, pin %d\n"</span><span class="token punctuation">,</span> group<span class="token punctuation">,</span> pin<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/* 100ask_IMX6uLL_Board LED：GPIO5_3 */</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">5</span> <span class="token operator">==</span> group<span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token punctuation">(</span><span class="token number">3</span> <span class="token operator">==</span> pin<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token comment" spellcheck="true">/* 点灯 */</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">==</span> status<span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token function">printk</span><span class="token punctuation">(</span><span class="token string">"&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;led on>>>>>>>>>>\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            val <span class="token operator">=</span> <span class="token function">readl</span><span class="token punctuation">(</span>GPIO5_DR<span class="token punctuation">)</span><span class="token punctuation">;</span>
            val <span class="token operator">&amp;</span><span class="token operator">=</span> <span class="token operator">~</span><span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">&lt;&lt;</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>    
            <span class="token function">writel</span><span class="token punctuation">(</span>val<span class="token punctuation">,</span> GPIO5_DR<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token comment" spellcheck="true">/* 灭灯 */</span>
        <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token number">0</span> <span class="token operator">==</span> status<span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token function">printk</span><span class="token punctuation">(</span><span class="token string">"&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;led off>>>>>>>>>>\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            val <span class="token operator">=</span> <span class="token function">readl</span><span class="token punctuation">(</span>GPIO5_DR<span class="token punctuation">)</span><span class="token punctuation">;</span>
            val<span class="token operator">|</span><span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">&lt;&lt;</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>    
            <span class="token function">writel</span><span class="token punctuation">(</span>val<span class="token punctuation">,</span> GPIO5_DR<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">else</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">else</span>
    <span class="token punctuation">{</span>
        <span class="token function">printk</span><span class="token punctuation">(</span><span class="token string">"This is not 100ask_IMX6ULL_Board!\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<h3 id="4、Makefile文件"><a href="#4、Makefile文件" class="headerlink" title="4、Makefile文件"></a>4、Makefile文件</h3><p><img src="https://s1.ax1x.com/2020/04/22/JUsNKs.png" alt></p>
<h2 id="运行测试"><a href="#运行测试" class="headerlink" title="运行测试"></a>运行测试</h2><p>这在文章开头的<code>体验设备树</code>一节中也有演示测试结果：</p>
<p><img src="https://s1.ax1x.com/2020/04/22/JUsUrn.png" alt><br><img src="https://s1.ax1x.com/2020/04/22/JUs0aV.jpg" alt><br><img src="https://s1.ax1x.com/2020/04/22/JUssGF.jpg" alt></p>
<p>话说我好像还没给板子露过面，这下点个灯露个面。</p>
<p>同时，在目录<code>/sys/firmware/devicetree/base</code>下，我们可以查看设备树节点：</p>
<p><img src="https://s1.ax1x.com/2020/04/22/JUswV0.png" alt></p>
<p>可以看到，我们创建的设备树节点<code>100ask_led@0</code>也在该目录下。<code>100ask_led@0</code>节点本身就是一个文件夹，可以使用cd命令进入该文件夹查看该节点的属性信息：</p>
<p><img src="https://s1.ax1x.com/2020/04/22/JUsabq.png" alt></p>
<p>属性值是字符串时，用 cat 命令可以打印出来；属性值是数值时，用 hexdump 命令可以打印出来。 </p>
<p>以上就是本次的实验分享。如有错误，欢迎指出！谢谢</p>
<hr>
<p>我的个人博客：<a href="https://www.lizhengnian.cn/">https://www.lizhengnian.cn/</a></p>
<p>我的微信公众号：嵌入式大杂烩</p>
<p>我的CSDN博客：<a href="https://blog.csdn.net/zhengnianli" target="_blank" rel="noopener">https://blog.csdn.net/zhengnianli</a></p>
<p><img src="https://s2.ax1x.com/2019/06/11/VcSFJJ.md.png" alt></p>

            </div>
            <hr>

            
            <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.88rem;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-large waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fa fa-close"></i></a>
            <h4 class="reward-title">欢迎关注ZhengN的微信公众号</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <!--<ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微信公众号</a></li>
                    </ul>-->
					<!--
                    <div id="alipay">
                        <img src="" class="reward-img" alt="支付宝打赏二维码">
                    </div>
					-->
                    <div id="wechat">
                        <img src="/medias/reward/wechat.jpg" class="reward-img" alt="微信公众号二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>
            

            <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">

<div id="article-share">
    
    <div class="social-share" data-disabled="qzone" data-wechat-qrcode-helper="<p>微信里点“发现”->“扫一扫”二维码便可查看分享。</p>"></div>
    
</div>

<script src="/libs/share/js/social-share.min.js"></script>

            


        </div>
    </div>

    

    

    

    

    
        <style>
    .valine-card {
        margin: 1.5rem auto;
    }

    .valine-card .card-content {
        padding: 20px 20px 5px 20px;
    }

    #vcomments input[type=text],
    #vcomments input[type=email],
    #vcomments input[type=url],
    #vcomments textarea {
        box-sizing: border-box;
    }

    #vcomments p {
        margin: 2px 2px 10px;
        font-size: 1.05rem;
        line-height: 1.78rem;
    }

    #vcomments blockquote p {
        text-indent: 0.2rem;
    }

    #vcomments a {
        padding: 0 2px;
        color: #42b983;
        font-weight: 500;
        text-decoration: underline;
    }

    #vcomments img {
        max-width: 100%;
        height: auto;
        cursor: pointer;
    }

    #vcomments ol li {
        list-style-type: decimal;
    }

    #vcomments ol,
    ul {
        display: block;
        padding-left: 2em;
        word-spacing: 0.05rem;
    }

    #vcomments ul li,
    ol li {
        display: list-item;
        line-height: 1.8rem;
        font-size: 1rem;
    }

    #vcomments ul li {
        list-style-type: disc;
    }

    #vcomments ul ul li {
        list-style-type: circle;
    }

    #vcomments table, th, td {
        padding: 12px 13px;
        border: 1px solid #dfe2e5;
    }

    #vcomments table, th, td {
        border: 0;
    }

    table tr:nth-child(2n), thead {
        background-color: #fafafa;
    }

    #vcomments table th {
        background-color: #f2f2f2;
        min-width: 80px;
    }

    #vcomments table td {
        min-width: 80px;
    }

    #vcomments h1 {
        font-size: 1.85rem;
        font-weight: bold;
        line-height: 2.2rem;
    }

    #vcomments h2 {
        font-size: 1.65rem;
        font-weight: bold;
        line-height: 1.9rem;
    }

    #vcomments h3 {
        font-size: 1.45rem;
        font-weight: bold;
        line-height: 1.7rem;
    }

    #vcomments h4 {
        font-size: 1.25rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    #vcomments h5 {
        font-size: 1.1rem;
        font-weight: bold;
        line-height: 1.4rem;
    }

    #vcomments h6 {
        font-size: 1rem;
        line-height: 1.3rem;
    }

    #vcomments p {
        font-size: 1rem;
        line-height: 1.5rem;
    }

    #vcomments hr {
        margin: 12px 0;
        border: 0;
        border-top: 1px solid #ccc;
    }

    #vcomments blockquote {
        margin: 15px 0;
        border-left: 5px solid #42b983;
        padding: 1rem 0.8rem 0.3rem 0.8rem;
        color: #666;
        background-color: rgba(66, 185, 131, .1);
    }

    #vcomments pre {
        font-family: monospace, monospace;
        padding: 1.2em;
        margin: .5em 0;
        background: #272822;
        overflow: auto;
        border-radius: 0.3em;
        tab-size: 4;
    }

    #vcomments code {
        font-family: monospace, monospace;
        padding: 1px 3px;
        font-size: 0.92rem;
        color: #e96900;
        background-color: #f8f8f8;
        border-radius: 2px;
    }

    #vcomments pre code {
        font-family: monospace, monospace;
        padding: 0;
        color: #e8eaf6;
        background-color: #272822;
    }

    #vcomments pre[class*="language-"] {
        padding: 1.2em;
        margin: .5em 0;
    }

    #vcomments code[class*="language-"],
    pre[class*="language-"] {
        color: #e8eaf6;
    }

    #vcomments [type="checkbox"]:not(:checked), [type="checkbox"]:checked {
        position: inherit;
        margin-left: -1.3rem;
        margin-right: 0.4rem;
        margin-top: -1px;
        vertical-align: middle;
        left: unset;
        visibility: visible;
    }

    #vcomments b,
    strong {
        font-weight: bold;
    }

    #vcomments dfn {
        font-style: italic;
    }

    #vcomments small {
        font-size: 85%;
    }

    #vcomments cite {
        font-style: normal;
    }

    #vcomments mark {
        background-color: #fcf8e3;
        padding: .2em;
    }

    #vcomments table, th, td {
        padding: 12px 13px;
        border: 1px solid #dfe2e5;
    }

    table tr:nth-child(2n), thead {
        background-color: #fafafa;
    }

    #vcomments table th {
        background-color: #f2f2f2;
        min-width: 80px;
    }

    #vcomments table td {
        min-width: 80px;
    }

    #vcomments [type="checkbox"]:not(:checked), [type="checkbox"]:checked {
        position: inherit;
        margin-left: -1.3rem;
        margin-right: 0.4rem;
        margin-top: -1px;
        vertical-align: middle;
        left: unset;
        visibility: visible;
    }
</style>

<div class="card valine-card" data-aos="fade-up">
    <div id="vcomments" class="card-content"></div>
</div>

<script src="/libs/valine/av-min.js"></script>
<script src="/libs/valine/Valine.min.js"></script>
<script>
    new Valine({
        el: '#vcomments',
        appId: 'Vc276uuqkd8y8heuqzShe9p1-gzGzoHsz',
        appKey: 'bUwy6lANpgeVdpcQamMtC8QD',
        notify: 'false' === 'true',
        verify: 'false' === 'true',
        visitor: 'true' === 'true',
        avatar: 'mm',
        pageSize: '10',
        lang: 'zh-cn',
        placeholder: '快来快活呀！^_^'
    });
</script>
    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6 overflow-policy" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fa fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2020/04/27/c-yu-yan-jing-dian-yi-cuo-de-jie-gou-ti-nei-cun-dui-qi-wen-ti/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/13.jpg" class="responsive-img" alt="【C语言】经典、易错的结构体内存对齐问题">
                        
                        <span class="card-title">【C语言】经典、易错的结构体内存对齐问题</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            好久没更新C语言文章了，今天给大家带来一道经典、易错的关于C语言结构体内存对齐的题目：
求32bit环境下以下结构体所占的字节数：
typedef struct test_struct
{
    char a;  
    short b
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="fa fa-clock-o fa-fw icon-date"></i>2020-04-27
                        </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/C语言/" class="post-category" target="_blank">
                                    C语言
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/结构体/" target="_blank">
                        <span class="chip bg-color">结构体</span>
                    </a>
                    
                    <a href="/tags/内存对齐/" target="_blank">
                        <span class="chip bg-color">内存对齐</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6 overflow-policy" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fa fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2020/04/11/linux-she-bei-shu-ji-chu-zhi-shi/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/14.jpg" class="responsive-img" alt="【Linux】设备树基础知识">
                        
                        <span class="card-title">【Linux】设备树基础知识</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            前言之前分享的笔记：【Linux笔记】总线设备驱动模型中在platform_device部分有简单说明描述设备有两种方法：一种是使用platform_device结构体来指定；另一种是使用设备树来描述。本篇笔记我们就来简单地学习一下设备树的
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="fa fa-clock-o fa-fw icon-date"></i>2020-04-11
                            </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/Linux/" class="post-category" target="_blank">
                                    Linux
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Linux/" target="_blank">
                        <span class="chip bg-color">Linux</span>
                    </a>
                    
                    <a href="/tags/驱动/" target="_blank">
                        <span class="chip bg-color">驱动</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>
</div>



    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title"><i class="fa fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fa fa-list"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            // headingsOffset: -205,
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).slideUp(500);
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).slideDown(500);
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>
    

</main>


<footer class="page-footer bg-color">
    <div class="container row center-align">
        <div class="col s12 m8 l8 copy-right">
            本站由&copy;<a href="https://zhengnianli.github.io/" target="_blank">ZhengNian</a>基于
            <a href="https://hexo.io/" target="_blank">Hexo</a> 的
            <a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">hexo-theme-matery</a>主题搭建.

            
                &nbsp;<i class="fa fa-area-chart"></i>&nbsp;站点总字数:&nbsp;
                <span class="white-color">187.2k</span>
            

            
			
                <br>
                
                <span id="busuanzi_container_site_pv">
                    <i class="fa fa-heart-o"></i>
                    本站总访问量 <span id="busuanzi_value_site_pv" class="white-color"></span>
                </span>
                
                
                <span id="busuanzi_container_site_uv">
                    <i class="fa fa-users"></i>
                    次,&nbsp;访客数 <span id="busuanzi_value_site_uv" class="white-color"></span> 人.
                </span>
                
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/zhengnianli" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fa fa-github"></i>
    </a>



    <a href="mailto:2238184039@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fa fa-envelope-open"></i>
    </a>



    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=2238184039" class="tooltipped" data-tooltip="QQ联系我: 2238184039" data-position="top" data-delay="50">
        <i class="fa fa-qq"></i>
    </a>



    <a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fa fa-rss"></i>
    </a>
</div>
    </div>
</footer>

<div class="progress-bar"></div>


<!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fa fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字" class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/js/search.js"></script>
<script type="text/javascript">
$(function () {
    searchFunc("/" + "search.xml", 'searchInput', 'searchResult');
});
</script>
<!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fa fa-angle-up"></i>
    </a>
</div>


<script src="/libs/materialize/materialize.min.js"></script>
<script src="/libs/masonry/masonry.pkgd.min.js"></script>
<script src="/libs/aos/aos.js"></script>
<script src="/libs/scrollprogress/scrollProgress.min.js"></script>
<script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
<script src="/js/matery.js"></script>

<!-- Global site tag (gtag.js) - Google Analytics -->



    <script src="/libs/others/clicklove.js"></script>


    <script async src="/libs/others/busuanzi.pure.mini.js"></script>


</body>
</html>